package com.example.home.youthstation;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class BotYIzhanAdapter extends RecyclerView.Adapter<BotYIzhanAdapter.MyHoldy> {
    private Context context;
    private List<BotYiZhanBean.RowsBean> list;

    public BotYIzhanAdapter(Context context, List<BotYiZhanBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_hostel,null);
        return new MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        Glide.with(context).load(ip+list.get(position).getCoverImgUrl()).into(holder.hostelImageView);
        Log.v("xhf","==="+ ip+list.get(position).getImageUrls());
        holder.hostelImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.bedsTextView.setText("剩余男床位:" + list.get(position).getBedsCountBoy());
        holder.bedsCountGirl.setText("剩余女床位:" + list.get(position).getBedsCountGirl());
        holder.hostelNameTextView.setText(list.get(position).getName());
        String address = list.get(position).getAddress();
        //地址内容截取
        if (address.length() >= 10) {
            // 如果内容超过10个字符，进行截断并添加"..."
            address = address.substring(0, 10) + "...";
        }
        holder.addressTextView.setText(address);
        // 处理站点介绍
        String introduce = list.get(position).getIntroduce();
        if (introduce.length() > 10) {
            // 如果内容超过10个字符，进行截断并添加"..."
            introduce = introduce.substring(0, 15) + "...";
        }
        holder.tv_introduce.setText(introduce);

        // 处理展开/收缩按钮点击事件
        holder.expandButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("展开".equals( holder.expandButton.getText().toString())) {
                    // 当前是展开状态，将其收缩
                    String address = list.get(position).getAddress();
                    holder.addressTextView.setText(address);

                    String introduce = list.get(position).getIntroduce();
                    holder.tv_introduce.setText(introduce);

                    holder.expandButton.setText("收缩");
                } else {
                    String address = list.get(position).getAddress();
                    if (address.length() >= 10) {
                        // 如果内容超过10个字符，进行截断并添加"..."
                        address = address.substring(0, 10) + "...";
                    }
                    holder.addressTextView.setText(address);
                    // 处理站点介绍
                    String introduce = list.get(position).getIntroduce();
                    if (introduce.length() > 10) {
                        // 如果内容超过10个字符，进行截断并添加"..."
                        introduce = introduce.substring(0, 15) + "...";
                    }
                    holder.tv_introduce.setText(introduce);
                    holder.expandButton.setText("展开");
                }
            }
        });




        holder.li_hostel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,HostelDetailActivity.class);
                intent.putExtra("RowsBeanId",String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });


    }

    private void setTextA(int status,TextView tv1,String txt1,TextView tv2,String txt2){

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 :list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        ImageView hostelImageView;
        TextView hostelNameTextView;
        TextView bedsTextView;
        TextView bedsCountGirl;
        TextView addressTextView;
        TextView tv_introduce;
        Button expandButton;
        RelativeLayout li_hostel;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            li_hostel = itemView.findViewById(R.id.li_hostel);
            hostelImageView = itemView.findViewById(R.id.hostelImageView);
            addressTextView = itemView.findViewById(R.id.addressTextView);
            hostelNameTextView = itemView.findViewById(R.id.hostelNameTextView);
            bedsTextView = itemView.findViewById(R.id.bedsTextView);
            tv_introduce = itemView.findViewById(R.id.tv_introduce);
            bedsCountGirl = itemView.findViewById(R.id.bedsCountGirl);
            expandButton = itemView.findViewById(R.id.expandButton);
            li_hostel = itemView.findViewById(R.id.li_hostel);
        }
    }
}
